package com.joe.demo.chat.server.handler;

import com.joe.demo.chat.server.factory.SessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description
 * @Author gaojw
 * @Date 2024-09-03
 */
@Slf4j
@ChannelHandler.Sharable
public class QuitHandler extends ChannelInboundHandlerAdapter {
    /**
     * 连接断开时，触发
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        final String username = SessionFactory.getSession().unbind(ctx.channel());
        log.info("{}, {} 已经断开了", username, ctx.channel());
    }

    /**
     * 异常断开时，触发
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("{} 已经异常断开，异常是 {}", ctx.channel(), cause.getMessage());
    }
}
